<template>
	<view class="content">
		<u--image :showLoading="true" src="../../../static/logo.jpg" width="80px" height="80px"
			@click="click"></u--image>
		<u-button type="primary" class="login-btn" @click="handleLogin" :loading="loading"
			loadingText="登录中">一键登录</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		wechatLogin
	} from '@/api/auth/user'
	export default {
		data() {
			return {
				title: 'Hello',
				loading: false
			}
		},
		//生命周期函数 它会在页面加载的时候自动触发
		onLoad() {
			//判断登录过没有
			if (uni.getStorageSync("token")) {
				//判断是否绑定手机号
				if (uni.getStorageSync('userInfo').phone) {
					//登陆过，跳转到面试页面
					uni.navigateTo({
						url: '/pages/interview/interview'
					})
				} else {
					uni.navigateTo({
						url: '/pages/bindphone/bindphone'
					})

				}


			}

		},
		methods: {
			//同步处理
			async handleLogin() {
				//改变登录状态
				this.loading = true
				//2.调用小程序登录，返回code
				const {
					code
				} = await uni.login({
					provider: 'weixin'
				})
				//3.调用后端接口，传入code，执行后端登录操作
				const res = await wechatLogin(code)
				console.log('res', res)
				if (res.code == 200) {
					//存入令牌
					uni.setStorageSync("token", res.token)
					//存入用户信息
					uni.setStorageSync("userInfo", res.userInfo)
					//判断是否绑定手机号
					if (!res.userInfo.phone) {
						//跳转到绑定手机号页面
						uni.navigateTo({
							url: '/pages/bindphone/bindphone'
						})
					} else {
						//跳转到模拟面试页面
						uni.navigateTo({
							url: '/pages/interview/interview'
						})
					}

				} else {
					//提示登录失败
					this.$refs.uToast.show({
						type: "error",
						icon: false,
						message: "登录失败"

					})
				}


				this.loading = false
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		text-align: center;
		margin-top: 300rpx;
	}

	.login-btn {
		margin-top: 100rpx;
		width: 300rpx;
	}
</style>